/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-8-27 下午12:49:52
 */
package com.absir.android.view;

import android.graphics.drawable.Drawable;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.absir.android.goods.GoodsContextUtils;
import com.absir.android.goods.GoodsInfo;
import com.absir.android.goods.IGoodsInfo;
import com.absir.android.inject.InjectResouceSupply;
import com.absir.android.view.annotation.InjectLayer;
import com.absir.android.view.annotation.InjectView;

/**
 * @author absir
 * 
 */
@InjectLayer
public class StoreItemView extends ViewHolder {

	@InjectView
	Button buy_button;

	@InjectView
	TextView name_text;

	@InjectView
	TextView price_text;

	@InjectView
	TextView count_text;

	@InjectView
	TextView desc_text;

	@InjectView
	ImageView icon_image;

	/**
	 * @param goodsInfo
	 */
	public void setGoodsInfo(IGoodsInfo goodsInfo, OnClickListener onClickListener) {
		setTag(goodsInfo);
		if (buy_button != null) {
			buy_button.setTag(this);
			buy_button.setOnClickListener(onClickListener);
		}

		if (name_text != null) {
			name_text.setText(goodsInfo.getGoodsName());

		} else if (buy_button != null) {
			buy_button.setText(goodsInfo.getGoodsName());
		}

		if (price_text != null) {
			price_text.setText(String.format("%.2f", goodsInfo.getPrice()));
		}

		if (desc_text != null) {
			desc_text.setText(goodsInfo.getGoodsDesc());
		}

		if (icon_image != null) {
			icon_image.setImageDrawable(getGoodsInfoDrawable(goodsInfo));
		}

		update();
	}

	/**
	 * 
	 */
	public void update() {
		GoodsInfo goodsInfo = (GoodsInfo) getTag();
		if (goodsInfo == null) {
			return;
		}

		if (count_text != null) {
			int count = GoodsContextUtils.getGoodsInfoCount(goodsInfo.getTag());
			count_text.setText(count >= 0 ? String.valueOf(count) : "");
		}
	}

	/**
	 * @param goodsInfo
	 * @return
	 */
	private Drawable getGoodsInfoDrawable(IGoodsInfo goodsInfo) {
		return InjectResouceSupply.getResouceObject(Drawable.class, InjectView.ID_NONE, null, "icon_" + (goodsInfo.getTag() instanceof Integer ? "gold" : String.valueOf(goodsInfo.getTag())));
	}
}
